package com.hucsmn.studentms;

import java.util.Objects;

/**
 * 学生信息类。
 */
public class Student implements Comparable<Student> {
    // 学号。
    private final int number;

    // 姓名。
    private final String name;

    // 年龄。
    private final int age;

    private Student(int number) {
        this.number = number;
        this.name = null;
        this.age = 0;
    }

    public Student(int number, String name, int age) {
        if (number < 0 || name == null || name.length() == 0 || age < 0) {
            throw new IllegalArgumentException();
        }
        this.number = number;
        this.name = name;
        this.age = age;
    }

    static Student getSearchKey(int number) {
        return new Student(number);
    }

    public int getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        // 按学号比较。
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return number == student.number;
    }

    @Override
    public int hashCode() {
        // 按学号比较。
        return Objects.hash(number);
    }

    @Override
    public int compareTo(Student o) {
        // 按学号比较。
        int c0, c1;
        return c0 = Integer.compare(number, o.number);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("【");
        sb.append("学号 ").append(number);
        sb.append("，姓名 '").append(name).append('\'');
        sb.append("，年龄 ").append(age);
        sb.append("】");
        return sb.toString();
    }
}

